<template>
	<!-- 项目大事记、房屋节点 编辑-->
	<view>
		<TopNavCustom :title="title"></TopNavCustom>
		<text class="time" style="margin-left: 30rpx; margin-top: 10rpx;">{{time}}</text>
		<view class="content">
			<u-picker :show="show" :columns="columns" @confirm="confirm" :title="pickerTitle" @close="()=>show=false"
				@cancel="()=>show=false" keyName="partName">
			</u-picker>
			<u-datetime-picker :show="showDateTime" v-model="dateTime" mode="datetime" closeOnClickOverlay
				@confirm="confirmDateTime" @cancel="()=>showDateTime=false" :title="pickerDateTitle">
			</u-datetime-picker>
			<view v-if="dynamicForm.items.length" class="dynamicForm">
				<u--form labelPosition="left" :model="dynamicForm" ref="refForm">
					<u-form-item v-for="(item, index) in dynamicForm.items" :key="item.key">
						<view class="add-zone">
							<view class="click-zone">
								<image src="/static/home_icons/delete.png" class="pix" @click.stop="removeCard(item)">
								</image>
								<image src="/static/home_icons/add.png" class="pix" @click.stop="addForm"></image>
							</view>
							<view class="add-form">
								<u-cell-group :border="false">
									<u-cell size="normal" title="目标位置" isLink v-model="item.estateName"
										@click="showPicker(0,item)">
									</u-cell>
									<u-cell size="normal" title="开工时间" v-model="item.startTime" isLink
										@click="showPicker(1,item)">
									</u-cell>
									<u-cell size="normal" title="结工时间" v-model="item.endTime" isLink
										@click="showPicker(2,item)">
									</u-cell>
									<u-cell size="normal" title="反馈描述" :border="false" :value="item.input_num +'/200'">
									</u-cell>
									<view class="pixs">
										<textarea class="_textarea" maxlength="200" @input="monitorInput(item)"
											v-model="item.feedBack" placeholder="请描述问题，最多输入200个字" />
										<!-- <span class="numLimit">{{item.input_num}}/200</span> -->
									</view>
									<u-cell size="normal" title="现场情况" :border="false"></u-cell>
									<view class="pixs">
										<u-upload :fileList="item.fileList" @afterRead="afterRead" name="pix"
											:maxCount="9" width="188rpx" height="188rpx" :previewFullImage="true"
											multiple @delete="deletePic" :itemData="item">
										</u-upload>
									</view>
								</u-cell-group>
							</view>
						</view>
					</u-form-item>
					<!-- <button type="default" class="btn" @click="goto(node.upload)" v-if="!node.upload">下一页</button> -->
					<!-- <button type="default" class="btn" @click="goto()">提交上传</button> -->
					<view class="btns">
						<button type="default" class="last" @click="goto('save')">暂存</button>
						<button type="default" class="next" @click="goto('push')">提交</button>
					</view>
				</u--form>
			</view>
			<view v-else>
				<view class="add-zone">
					<view class="click-zone">
						<image src="/static/home_icons/delete.png" class="pix" @click.stop=""></image>
						<image src="/static/home_icons/add.png" class="pix" @click.stop="addForm()"></image>
					</view>
				</view>
				<u-empty text="暂无记录~" icon="/static/home_icons/nodata.png" marginTop="166rpx" width="200rpx"
					height="131rpx">
				</u-empty>
			</view>
		</view>

	</view>
</template>

<script>
	import _utils from "@/common/_utils.js"
	import _public from "@/common/public.js"
	import _file from "@/common/file.js"
	import _project from "@/common/project.js"
	export default {
		onLoad(node) {
			this._node = JSON.parse(decodeURIComponent(node.item))
			this.title = this._node.partName
		},
		data() {
			return {
				time: uni.$u.timeFormat(false, 'yyyy-mm-dd'),
				title: "",
				_node: {},
				dynamicForm: {
					items: []
				},
				fileIds: [],
				flag: 0,
				formList: [],
				fileList: [],
				columns: [],
				pickerTitle: '',
				showEmpty: true,
				show: false,
				partOne: [],
				dateTime: Number(new Date()),
				showDateTime: false,
				temp: 0,
				pickerDateTitle: ''
			};
		},
		created() {
			this.Init()
		},
		methods: {
			monitorInput(item) {
				this.dynamicForm.items[this.temp].input_num = item.feedBack.length
			},
			async Init() {
				// let _node = await _project.getPartNode(this._node.partId)
				// this.partOne = _node
				// this.columns = [this.partOne]
				//获取节点列表
				let _node = await _public.getHouseNodeColumns()
				this.partOne = _node
				this.columns = [this.partOne]
			},
			removeCard(data) {
				let index = this.dynamicForm.items.indexOf(data)
				if (index !== -1) {
					this.dynamicForm.items.splice(index, 1)
				}
			},
			addForm() {
				this.dynamicForm.items.push({
					key: Date.now(),
					createTime: uni.$u.timeFormat(false, 'yyyy-mm-dd hh:MM:ss'),
					estateId: "",
					partId: this._node.partId,
					type: 2,
					eventStatus: 1,
					issueTags: [],
					fileList: [],
					flag: this._node.flag,
					input_num: 0,
					startTime: "",
					endTime: '',
					feedBack: "",
					estateName: ""
				})
			},
			goto(tip) {
				//数据校验
				let flag = true
				let _form_data = this.dynamicForm.items
				for (let i = 0; i < _form_data.length; i++) {
					let j = i + 1
					if (_form_data[i].estateName == '') {
						uni.$showMsg('第' + j + '组尚未选择目标位置')
						flag = false
						break
					} else if (_form_data[i].startTime == '') {

						uni.$showMsg('第' + j + '组尚未选择开始时间')
						flag = false
						break
					} else if (_form_data[i].endTime == '') {
						uni.$showMsg('第' + j + '组尚未选择结束时间')
						flag = false
						break
					} else if (_form_data[i].feedBack == '') {
						uni.$showMsg('第' + j + '组尚填写描述信息')
						flag = false
						break
					} else if (_form_data[i].fileList == '') {
						uni.$showMsg('第' + j + '组尚未上传现场图片')
						flag = false
						break
					}
				}
				if (flag) {
					let cloneData = uni.$u.deepClone(this.dynamicForm.items)
					for (let i = 0; i < cloneData.length; i++) {
						let pixs = []
						for (let j = 0; j < cloneData[i].fileList.length; j++) {
							if ('push' == tip) {
								cloneData[i].eventStatus = 2
							} else {
								cloneData[i].eventStatus = 1
							}
							pixs.push(cloneData[i].fileList[j].urlId)
						}
						cloneData[i].images = pixs.toString()
					}
					if (_project.pushEventsArray(cloneData)) {
						if (tip == 'save') {
							uni.$showMsg('数据保存成功')
						} else {
							uni.$showMsg('上传成功')
						}
						uni.$u.sleep(800).then(() => {
							uni.switchTab({
								url: '/pages/ihome/home/home'
							})
						})
					}
				}
			},
			// 删除图片
			deletePic(event) {
				let i = this.dynamicForm.items.indexOf(event.itemData)
				this.dynamicForm.items[i].fileList.splice(event.index, 1)
				uni.$showMsg("删除成功")
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let index = this.dynamicForm.items.indexOf(event.itemData)
				let lists = [].concat(event.file)
				let fileListLen = this.dynamicForm.items[index].fileList.length
				lists.map((item) => {
					this.dynamicForm.items[index].fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await _file.uploadFilePromise(lists[i].url)
					const temp = JSON.parse(result)
					let item = this.dynamicForm.items[index].fileList[fileListLen]
					this.dynamicForm.items[index].fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						urlId: temp.body.fileId
					}))
					fileListLen++
				}
			},
			confirmDateTime(time) {
				if (this.flag == '1') {
					this.dynamicForm.items[this.temp].startTime = uni.$u.timeFormat(time.value,
						'yyyy-mm-dd hh:MM:ss')
				} else {
					this.dynamicForm.items[this.temp].endTime = uni.$u.timeFormat(time.value,
						'yyyy-mm-dd hh:MM:ss')
				}

				this.showDateTime = false
			},
			confirm(item) {
				switch (this.flag) {
					case 0:
						this.dynamicForm.items[this.temp].estateName = item.value[0].partName
						this.dynamicForm.items[this.temp].estateId = item.value[0].partId
						// this.dynamicForm.items[this.temp].partName = item.value[0].partName
						// this.dynamicForm.items[this.temp].partId = item.value[0].partId
						break;
				}
				this.show = false
			},
			showPicker(item, data) {
				let index = this.dynamicForm.items.indexOf(data)
				this.temp = index
				this.flag = item
				switch (this.flag) {
					case 0:
						this.pickerTitle = '选择目标区域'
						this.columns = [this.partOne]
						this.show = true
						break;
					case 1:
						this.pickerDateTitle = '选择开工时间'
						this.showDateTime = true
						break;
					case 2:
						this.pickerDateTitle = '选择结工时间'
						this.showDateTime = true
						break;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.process-bar {
		// display: block;
		border: 1rpx solid #efefef;
		border-radius: 20rpx;
		background-color: #FFFFFF;
		padding: 30rpx;
		margin-bottom: 20rpx;

		.steps {
			// display: block;
			margin-top: 45rpx;
		}
	}

	.content {
		padding: 20rpx 30rpx 58rpx 30rpx;
		display: flex;
		flex-direction: column;

		.dynamicForm {
			display: block;

			.add-zone {
				width: 100%;
			}
		}


	}

	.add-zone {
		padding: 20rpx;
		border: 1rpx solid #efefef;
		border-radius: 20rpx;
		background-color: #FFFFFF;
	}

	.add-form {
		// display: flex;
		// flex-direction: column;
		// display: block;

		.pixs {
			padding: 20rpx;

		}
	}

	.click-zone {
		display: flex;
		flex-direction: row;
		justify-content: space-between;

		.pix {
			width: 40rpx;
			height: 40rpx;
		}
	}

	// .btn {
	// 	margin-top: 60rpx;
	// 	width: 560rpx;
	// 	background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
	// 	box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
	// 	border-radius: 44rpx;
	// 	color: #FFFFFF;
	// }
	.btns {
		display: block;
		margin-top: 88rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-around;

		.last {
			color: #E62029;
			width: 300rpx;
			background: #FFFFFF;
			border: 2rpx solid #E62029;
			border-radius: 44rpx;
		}

		.next {
			color: #FFFFFF;
			width: 300rpx;
			background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
			box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 44rpx;
		}
	}

	._textarea {
		// width: 620rpx;
		height: 160rpx;
		background: #F5F5F5;
		border-radius: 10rpx;
	}

	.numLimit {
		position: relative;
		top: -40rpx;
		right: -530rpx;

	}
</style>
